<!DOCTYPE html>
<html>
<head>
    <meta name="screen-orientation" content="portrait">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"/>
    <title>年会抽奖小程序</title>
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/wall.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <style type="text/css">
        /* 设置滚动条的样式 */
        ::-webkit-scrollbar {
            width:12px;
        }
        /* 滚动槽 */
        ::-webkit-scrollbar-track {
            -webkit-box-shadow:inset006pxrgba(0,0,0,0.3);
            border-radius:10px;
        }
        /* 滚动条滑块 */
        ::-webkit-scrollbar-thumb {
            border-radius:10px;
            background:rgba(0,0,0,0.1);
            -webkit-box-shadow:inset006pxrgba(0,0,0,0.5);
        }
        ::-webkit-scrollbar-thumb:window-inactive {
            background:rgba(255,0,0,0.4);
        }

        body, html {
            width: 100%;
            height: 100%;
        }
        .result {
            position: absolute;
            height: 400px;
            /*width: 100%;*/
            width: 60%;
            left: 50%; top: 50%;
            margin-top: -200px;
            margin-left: -30%;
            text-align: center;
            padding: 20px;
            display: none;
            background-color:#FFC300;
            border-radius: 5px;
        }
        .result span {
            display: inline-block;
            font-size: 16px;
            width: 220px;
            /*background: #fff;*/
            line-height: 35px;
            background-image: url("img/bg.png");
            background-repeat: no-repeat;
            color: #000;
            margin: 5px;
            /*border-radius: 10px;*/
            /*box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);*/
            padding: 10px 20px;
        }
        .result-title{
            font-size:35px;
            padding: 20px;
            color:#A04000;
        }
        .myCanvas{
            position: absolute;
            left:0;
            height:100%;
            width:60%;
        }
        .board{
            position: absolute;
            right:2%;
            top: 15%;
            height:70%;
            width:36%;
            /*background-color:#ffc300d9;*/
            border-radius: 30px;
            text-align: center;
            margin: 30px;
            display: none;
        }
        .board-title{
            color:white;
            font-size: 40px;
            height: 10%;
            padding: 20px;
        }
        .board-content-title{
            font-size:32px;
            padding: 20px;
            color:#FFFFFF;
        }
        .board-content{
            display: inline-block;
            display: none;
            height: 90%;
            text-align: center;
            overflow-x:hidden;
            overflow-y:auto;

        }
        .board span{
            display: inline-block;
            font-size: 16px;
            width: 220px;
            /*background: #fff;*/
            line-height: 35px;
            background-image: url("img/bg.png");
            background-repeat: no-repeat;
            color: #000;
            margin: 5px;
            /*border-radius: 10px;*/
            /*box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);*/
            padding: 10px 20px;
        }
        button, input, optgroup, select, textarea {
            color: inherit;
            font: inherit;
            margin: 0;
            border: none;
        }
        button {
            overflow: visible;
        }
        button, select {
            text-transform: none;
        }
        button, html input[type=button], input[type=reset], input[type=submit] {
            -webkit-appearance: button;
            cursor: pointer;
        }


        .pure-button {
            display: inline-block;
            zoom: 1;
            line-height: normal;
            white-space: nowrap;
            vertical-align: middle;
            text-align: center;
            cursor: pointer;
            -webkit-user-drag: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }

        .pure-button {
            font-family: inherit;
            font-size: 100%;
            padding: .5em 1em;
            color: #444;
            color: rgba(0,0,0,.8);
            border: 0 rgba(0,0,0,0);
            background-color: #E6E6E6;
            text-decoration: none;
            border-radius: 2px;
        }
        .pure-button:focus {
            outline: 0
        }
        .pure-button-hover, .pure-button:hover, .pure-button:focus {
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#1a000000', GradientType=0);
            background-image: -webkit-gradient(linear,0 0,0 100%,from(transparent),color-stop(40%,rgba(0,0,0,.05)),to(rgba(0,0,0,.1)));
            background-image: -webkit-linear-gradient(transparent,rgba(0,0,0,.05) 40%,rgba(0,0,0,.1));
            background-image: -moz-linear-gradient(top,rgba(0,0,0,.05) 0,rgba(0,0,0,.1));
            background-image: -o-linear-gradient(transparent,rgba(0,0,0,.05) 40%,rgba(0,0,0,.1));
            background-image: linear-gradient(transparent,rgba(0,0,0,.05) 40%,rgba(0,0,0,.1));
        }

        .button-success, .button-error, .button-warning, .button-secondary {
            color: white;
            border-radius: 4px;
            text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
        }
        .button-success {
            background: rgb(28, 184, 65);
        }
        .button-error {
            background: rgb(202, 60, 60);
        }
        .button-warning {
            background: rgb(223, 117, 20);
        }
        .button-secondary {
            background: rgb(66, 184, 221);
        }
        .setting {
            position: absolute;
            top: 20px;
            left: 20px;
            text-align: center;
        }
        .selects {
            position: absolute;
            bottom: 20px;
            left: 20px;
            text-align: center;
        }
        .reset {
            position: absolute;
            top: 20px;
            right: 20px;
            text-align: center;
        }
        .reset .pure-button {
            display: inline-block;
            margin: 5px;
            padding: 10px 0;
            text-align: center;
            width:50px;
        }
        .tools {
            position: absolute;
            bottom: 20px;
            right: 20px;
            text-align: center;
        }
        .tools .pure-button {
            display: inline-block;
            margin: 5px;
            padding: 10px 0;
            text-align: center;
            width:50px;
        }
        .mask {
            -webkit-filter:blur(5px);
        }
        #main {
            -webkit-transition: all 1s;
            transition: all 1s;
        }
    </style>
</head>
<body>
<div id="main" class="wall" v-on="click:onClick">
    <div id="board" class="board" style="display: block">
        <div class="board-title">Winner List</div>
        <div id="board-content" class="board-content" style="display: block">
        </div>
    </div>
</div>
<div id="result" class="result">
</div>
<!--<div id="setting" class="setting">-->
    <!--<button class="pure-button button-secondary" v-on="click: setting">设置</button>-->
<!--</div>-->
<div id="selects" class="selects">
    <select id="select-input"
            v-on="change:choose"
            class="form-control"
            >
        <option disabled selected value="0">Please Select</option>
        <option id="backup" value="1">Options - (1)</option>
        <option
         v-repeat="prize:prizes"
         value="{{prize.num}}">{{prize.name}} - ({{prize.num}})</option>
    </select>
        <!--<button-->
        <!--v-on="click: onClick($value)"-->
        <!--class="pure-button"-->
        <!--v-class="button-error: selected == $value"-->
        <!--v-repeat="btns">{{$value}}</button>-->
    </div>
<div id="reset" class="reset">
    <button class="pure-button button-warning" v-on="click: reset">Reset</button>
</div>
<div id="tools" class="tools">
    <button id="startbutton"
        class="pure-button"
        v-on="click: toggle"
        v-class="
                button-secondary: !running,
                button-success: running
            ">{{running?'Stop!':'Start'}}</button>
</div>
<script type="text/javascript" src="js/zepto.js"></script>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript" src="js/tagcanvas.js"></script>
<script type="text/javascript" src="js/xlsx.core.min.js"></script>
<!--<script type="text/javascript" src="js/member.js"></script>-->
<script type="text/javascript">

    (function(){
        var member=[];
        var choosed = JSON.parse(localStorage.getItem('choosed')) || {};
        var boardContent = localStorage.getItem('boardContent')||'';

        console.log(choosed);
        var speed = function(){
            return [0.1 * Math.random() + 0.01, -(0.1 * Math.random() + 0.01)];
        };

        var readWorkbookFromRemoteFile = function(url, callback,val) {
            var xhr = new XMLHttpRequest();
            xhr.open('get', url, true);
            xhr.responseType = 'arraybuffer';
            xhr.onload = function(e) {
                if(xhr.status == 200) {
                    var data = new Uint8Array(xhr.response)
                    var workbook = XLSX.read(data, {type: 'array'});
                    if(callback) callback(workbook,val);
                }
            };
            xhr.send();
        }



        var getKey = function(item){
            return item.name + '-' + item.phone;
        };
        var createHTML = function(){
            var html = [ '<ul>' ];
            member.forEach(function(item, index){
                item.index = index;
                var key = getKey(item);
                var color = choosed[key] ? 'yellow' : 'white';
                html.push('<li><a href="#" style="color: ' + color + ';">' + item.name + '</a></li>');
            });
            html.push('</ul>');
            return html.join('');
        };



        var lottery = function(count){
            var total = member.length;
            var ret = [];
            var list = canvas.getElementsByTagName('a');
            var color = '#' + ('00000' + Math.floor(Math.random() * 0xffffff)).slice(-6);
            var color = 'yellow';
            var isChoosed = 0;
            for(let i = 0; i<total;i++){
                var key = getKey(member[i]);
                if(!member[i].inner)
                {
                    choosed[key]=1;
                }
                if((!choosed[key])&&member[i].inner)
                {
                    isChoosed++;
                }
            }

            if(isChoosed>0)
            {
                if(count>isChoosed)
                {
                    count = isChoosed;
                }
                for(let i = 0; i < count; i++){
                do {
                    var id = Math.floor(Math.random() * total);
                    if(!member[id].inner)
                        continue;
                    else if(member[id]){
                        var key = getKey(member[id]);
                    }
                } while(choosed[key]);
                choosed[key] = 1;
                    ret.push(member[id].name);
                // ret.push(member[id].name + '<br/>' + member[id].phone);
                list[id].style.color = color;
            }
            localStorage.setItem('choosed', JSON.stringify(choosed));
                return ret;
            }
            else
            {

                alert('failed');
                // return ret;
            }
            
        };
        var canvas = document.createElement('canvas');
        canvas.id = 'myCanvas';
        // canvas.setAttribute('class','myCanvas') ;
        canvas.width = document.body.offsetWidth*0.6;
        canvas.height = document.body.offsetHeight;
        document.getElementById('main').appendChild(canvas);
        var g_selected = 0;
        var g_prize_title = '';

        var outputWorkbook =   function(workbook,obj) {
            var usersheet = workbook.Sheets['抽奖名单']; // 只能通过工作表名称来获取指定工作表
            var user = XLSX.utils.sheet_to_json(usersheet);
            var prizesheet = workbook.Sheets['抽奖奖品']; // 只能通过工作表名称来获取指定工作表
            let prize = XLSX.utils.sheet_to_json(prizesheet);
            // console.log(user);
            obj.prizes = [];
            obj.prizes = obj.prizes.concat(prize);
            console.log(user);
            member = user;
            canvas.innerHTML = createHTML();
            TagCanvas.Start('myCanvas', '', {
                textColour: null,
                initial: speed(),
                dragControl: 1,
                textHeight: 14
            });
        }

        new Vue({
            el:'#main',
            methods:{
                onClick:function () {
                    $('#result').css('display', 'none');
                    $('#main').removeClass('mask');
                }
            }
        })
        new Vue({
            el: '#selects',
            data: {
                selected: 0,
                running: false,
                // btns: [
                //     10, 5, 2, 1
                // ],
                prizes:[
                    {name:'一等奖',num:1},
                    {name:'二等奖',num:2},
                    {name:'三等奖',num:3},
                    {name:'四等奖',num:4},
                    {name:'五等奖',num:5},
                    {name:'六等奖',num:6}
                ]
            },
            ready: function(){
                readWorkbookFromRemoteFile('file/config.xlsx',outputWorkbook,this);
                $('#board-content').html(boardContent);

            },
            methods: {
                onClick: function(num){
                    $('#result').css('display', 'none');
                    $('#main').removeClass('mask');
                    this.selected = num;
                    g_selected = num;
                },
                choose: function(el){
                    $('#result').css('display', 'none');
                    $('#main').removeClass('mask');
                    let num = el.target.value;
                    let prize = $("#select-input option:selected");
                    this.selected = num;
                    g_selected = num;
                    g_prize_title = prize.text();
                    $("#startbutton").removeAttr("disabled");
                }
            }
        });
        new Vue({
            el: '#reset',
            data: {
                selected: 0,
                running: false,
            },
            methods: {
                reset: function(){
                    if(confirm('Are you sure to reset all？It will delete all the history！')){
                        localStorage.removeItem('boardContent');
                        localStorage.removeItem('choosed');
                        // localStorage.clear();
                        location.reload(true);
                    }
                }
            }
        });
        new Vue({
            el: '#tools',
            data: {
                selected: 0,
                running: false,
                btns: [
                    10, 5, 2, 1
                ]
            },
            methods: {
                toggle: function(){
                    if(g_selected === 0)
                    {
                        alert("Please select the prize");
                        return;
                    }
                    if(this.running){
                        TagCanvas.SetSpeed('myCanvas', speed());
                        var ret = lottery(g_selected);

                        $('#result').css('display', 'block').html('<div class="result-title">'+g_prize_title+'</div>'+'<span>' + ret.join('</span><span>') + '</span>');
                        localStorage.setItem('boardContent','<div class="board-content-title">'+g_prize_title+'</div>'+'<span>' + ret.join('</span><span>') + '</span>'+boardContent);
                        boardContent = localStorage.getItem('boardContent');
                        $('#board-content').html(boardContent);
                        TagCanvas.Reload('myCanvas');
                        setTimeout(function(){
                            localStorage.setItem(new Date().toString(), JSON.stringify(ret));
                            $('#main').addClass('mask');
                        }, 300);

                        if($("#select-input option:selected").attr("id") !='backup')
                        {
                            $("#select-input option:selected").attr("disabled",'disabled');
                            $("#startbutton").attr("disabled",'disabled');
                            g_selected = 0;
                        }

                    } else {
                        $('#result').css('display', 'none');
                        $('#main').removeClass('mask');
                        TagCanvas.SetSpeed('myCanvas', [5, 1]);
                    }
                    this.running = !this.running;
                }
            }
        });
    })();
</script>
</body>
</html>